﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Jb.Domain;
using Jb.Web.ViewModels;

namespace Jb.Web.Utility
{
    public static class JobUtil
    {
        public static void SetJobLocation(Job entity, JobViewModel viewModel)
        {
            viewModel.JobLocation = String.Format("{0}, {1}, {2}", entity.City.Name, entity.City.State.Name, entity.City.State.Country.Name);
        }
        public static void SetJobLocation(Job entity, JobViewModel viewModel, String separator)
        {
            viewModel.JobLocation = String.Format("{1}{0}{2}{0}{3}", separator, entity.City.Name, entity.City.State.Name, entity.City.State.Country.Name);
        }

        #region Parent/Child category selection

        /// <summary>
        /// Gets all related Categories by a CategoryID, searches only in category cache
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static String GetRelatedCategories(int categoryID)
        {
            CategoryViewModel category = ApplicationCache.Categories.FirstOrDefault(C => C.ID == categoryID);
            return GetRelatedCategories(category);
        }

        /// <summary>
        /// Gets all related categories by a categoryUrlName, searches only in the cache
        /// </summary>
        /// <param name="categoryUrlName"></param>
        /// <returns></returns>
        public static String GetRelatedCategories(String categoryUrlName)
        {
            CategoryViewModel category = ApplicationCache.Categories.FirstOrDefault(C => String.Compare(C.UrlName, categoryUrlName, true) == 0);
            return GetRelatedCategories(category);

        }

        /// <summary>
        /// Gets all related categories by a Category Object
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public static String GetRelatedCategories(CategoryViewModel category)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(10);            
            if (category != null)
            {
                ///Append the selected category at first
                sb.Append(category.ID);
                ///Appdend the Chaild categories if they exist
                if (category.ChildCategories.Count > 0)
                {
                    foreach (var childCategory in category.ChildCategories)
                    {
                        sb.AppendFormat(",{0}", childCategory.ID);
                    }
                }
                //else
                //    sb.Append(category.ID);
            }
            return sb.ToString();
        }

        #endregion
    }
}